package com.eatme.core.map;

import java.util.ArrayList;
import java.util.Random;

// 有序映射 基于搜索树的实现
//无序映射 基于哈希表的实现
//多重映射
public interface Map<K,V> {
    void add(K key,V value);
    V remove(K k);

    boolean contains(K key);

    V get(K key);

    void set(K key,V newValue);

    int getSize();

    boolean isEmpty();

    public static double testMap(Map<Integer,Integer> map,ArrayList<Integer> arrayList) {
        long startTime = System.nanoTime();
        //System.out.println(arrayList);
        System.out.println("Total nums: "+arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            if (map.contains(arrayList.get(i)))
                map.set(arrayList.get(i),map.get(arrayList.get(i))+1);
            else
                map.add(arrayList.get(i),1);

        }
        System.out.println("Total different numbers: " +map.getSize());
        System.out.println("Frequency of 0: " +map.get(0));
        System.out.println("Frequency of 100: " +map.get(100));
        long endTime = System.nanoTime();
        return  (endTime-startTime)/ 1000000.0;
    }

    public static void main(String[] args) {
        int n = 100000;
        ArrayList<Integer> arrayList = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < n ; i++) {
            arrayList.add(random.nextInt(n));
        }
        Map<Integer,Integer> map = new BSTMap<>();
        Map<Integer,Integer> map1 = new LinkedListMap<>();
        double msTime = testMap(map,arrayList);
        System.out.println("BSTMap: " +msTime+" ms");
        System.out.println();
        double msTime1 =testMap(map1,arrayList);
        System.out.println("LinkedListMap: " +msTime1+" ms");

    }


}
